package nl.hanze.designpatterns.DAO.impl.db;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import nl.hanze.designpatterns.DAO.TroubleTicketDAO;
import nl.hanze.designpatterns.db.Executor;
import nl.hanze.designpatterns.domain.TroubleTicket;

public class TroubleTicketDAOImpl implements TroubleTicketDAO {

	@Override
	public List<TroubleTicket> getTroubleTicketsByName(String callerName) throws Exception{
		TroubleTicket ticket = new TroubleTicket(null,null,null,null);
		List<TroubleTicket> list = new ArrayList<TroubleTicket>();
		Executor ex = Executor.getInstance();
		String query ="SELECT * FROM troubleTicket WHERE callername='"+callerName+"'";
		Iterator <?> iterator = ex.getIterator(query,ticket.getClass());
		while(iterator.hasNext()){
			ticket = (TroubleTicket)iterator.next();
			list.add(ticket);
		}
		return list;
	}

	@Override
	public void saveTroubleTicket(TroubleTicket troubleticket){
		Executor ex = Executor.getInstance();
		String query ="INSERT INTO `designpatterns`.`troubleticket` (`userName`, `callerName`, `description`,`dateTime`) VALUES ('"+troubleticket.getUserName()+"', '"+troubleticket.getCallerName()+"', '"+troubleticket.getDescription()+"', '"+troubleticket.getDateTime()+"');";
		try {
			ex.executeQuery(query);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
